using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qVAKOLL = from m in mtemp.VAKOLL select m;
       DBMapper.MapVAKOLL(qVAKOLL.ToList(), true);
       */
        public List<VAKOLL> MapVAKOLL(XElement item2, bool mergedb)
        {
            List<VAKOLL> result = new List<VAKOLL>();
            var item4 = from item3 in item2.Elements()
                        select new VAKOLL
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            VAKMANDANT = (string)item3.Element("VAKMANDANT"),
                            VAKKOLL = (string)item3.Element("VAKKOLL"),
                            VAKBEZ = (string)item3.Element("VAKBEZ"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapVAKOLL(result, mergedb);
          return result;
        }
        ///DBMapper.MapVAKOLL(mtemp.VAKOLL.ToList<VAKOLL>(), true);        
       public void MapVAKOLL(List<VAKOLL> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from VAKOLL row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.VAKOLL.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.VAKOLL.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.VAKOLL.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "VAKOLL")]
    public class VAKOLL : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _VAKMANDANT="0";
        [Column]
        public string VAKMANDANT
        {
            get
            {
                return _VAKMANDANT;
            }
            set
            {
                if (value != _VAKMANDANT)
                {
                     _VAKMANDANT = value;
                    NotifyPropertyChanged("VAKMANDANT");
                }
            }
        }
        private string  _VAKKOLL="0";
        [Column]
        public string VAKKOLL
        {
            get
            {
                return _VAKKOLL;
            }
            set
            {
                if (value != _VAKKOLL)
                {
                     _VAKKOLL = value;
                    NotifyPropertyChanged("VAKKOLL");
                }
            }
        }
        private string  _VAKBEZ="0";
        [Column]
        public string VAKBEZ
        {
            get
            {
                return _VAKBEZ;
            }
            set
            {
                if (value != _VAKBEZ)
                {
                     _VAKBEZ = value;
                    NotifyPropertyChanged("VAKBEZ");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<VAKOLL> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\VAKOLL\\VAKOLL" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\VAKOLL" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("VAKOLL");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (VAKOLL row in ListInput)
                {
                    XElement f = new XElement("VAKOLL");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement VAKMANDANT = new XElement("VAKMANDANT");
                         VAKMANDANT.Value = row.VAKMANDANT;
                          f.Add(VAKMANDANT);
                        XElement VAKKOLL = new XElement("VAKKOLL");
                         VAKKOLL.Value = row.VAKKOLL;
                          f.Add(VAKKOLL);
                        XElement VAKBEZ = new XElement("VAKBEZ");
                         VAKBEZ.Value = row.VAKBEZ;
                          f.Add(VAKBEZ);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
